var ID;
$(function () {
    <!--弹出层插件-->
    layui.use('layer', function(){
        var layer = layui.layer;
    });
    findByItem(1)
});

var form;
layui.use('form', function(){
    form= layui.form;

});


function findByItem(pageNO) {
    //初始化表单数据
    $.post("/supplier/findByItem/",$("#formId").serialize()+"&pageNO="+pageNO,function (pageInfoList) {
        var str = "";
        for (var i = 0; i < pageInfoList.list.length; i++) {
            var obj = pageInfoList.list[i]
            str += `<tr><td>${obj.name}</td><td>${obj.headMan}</td><td>${obj.scope}</td><td> <a class="layui-btn layui-btn-warm" href="javascript:update(${obj.id})" style="width: 85px" >修改信息</a>
                    <a class="layui-btn layui-btn-warm" href="javascript:findByid(${obj.id})" style="width: 85px;margin-left: 10px">查看信息</a></td></tr>`
        }
        $("#tbodyId").html(str)
        if(pageNO==1){
            layui.use('laypage', function () {
                var laypage = layui.laypage;
                laypage.render({
                    elem: "cutPageDiv"
                    , count: pageInfoList.total,
                    limit: 4,
                    jump: function (obj, first) {
                        /*打印页码*/
                        if (first != 1) {
                            findByItem(obj.curr);
                        }
                    }
                })
            })
        }
    })
}

function add() {
    layer.open({
        title:"⚠",
        type:1,//弹出层的类别，1表示页面层
        shadeClose:true,//点击关闭遮罩框
        skin:'layui-layer-rim',
        area:['800px','600px'],//宽高
        content:$("#add"),
        anim: 6
    });
}
function closeAdd() {
    $.post("/supplier/add",$("#addFrom").serialize(),function (info) {
        if(info=='ok'){
            layer.closeAll('page');
            location.href="/html/ziliao/supplier/supplier.html"
        }else {
            var errorlist=JSON.parse(info)
            var st=""
            for (var i=0;i<errorlist.length;i++){
                var errorobj=errorlist[i];
                st=`<div style="color: orangered">${errorobj.defaultMessage}</div>`
                $("#"+errorobj.field+"Span").html(st);
            }
        }
    })
}
function update(id) {
    ID=id
    $.post("/supplier/findById","id="+id,function (customer) {
        $("#headMan").val(customer.headMan);
        $("#tel").val(customer.tel)
        $("#scopeinput").html(customer.scope)
        $("#nameinput").html(customer.name)
        $("#addressinput").html(customer.address)
        $("#emailinput").html(customer.email)
    })
    layer.open({
        title:"⚠",
        type:1,//弹出层的类别，1表示页面层
        shadeClose:true,//点击关闭遮罩框
        skin:'layui-layer-rim',
        area:['800px','600px'],//宽高
        content:$("#update"),
    });
}
function closeUpdate() {
    $.post("/supplier/update/",$("#updateFrom").serialize()+"&id="+ID,function (info) {
        if(info =="ok"){
            layer.closeAll('page');
            location.href="/html/ziliao/supplier/supplier.html"
        }else {
            var errorlist=JSON.parse(info)
            var st=""
            for (var i=0;i<errorlist.length;i++){
                var errorobj=errorlist[i];
                st=`<div style="color: orangered">${errorobj.defaultMessage}</div>`
                $("#"+errorobj.field+"input").html(st);
            }

        }

    })
}
function findByid(id) {
    $.post("/supplier/findById","id="+id,function (supplier) {
        $("#namefind").html(supplier.name);
        $("#headManfind").html(supplier.headMan)
        $("#scopefind").html(supplier.scope)
        $("#telfind").html(supplier.tel)
        $("#addressfind").html(supplier.address)
    })
    layer.open({
        title:"⚠",
        type:1,//弹出层的类别，1表示页面层
        shadeClose:true,//点击关闭遮罩框
        skin:'layui-layer-rim',
        area:['800px','600px'],//宽高
        content:$("#find"),
    });
}
function closefind() {
    layer.closeAll('page');
}
function t4() {
    findByItem(1)
}